/**
 * 优化和增加功能的 Bukkit 插件模板.
 * 修改了配置文件编码设置,强制使用UTF-8.
 * 并且支持多配置文件.
 * 增加getInstance函数.
 * 增加兼容全版本的 getPlayers 函数.
 * 联动 Message 类.
 */
package com.qq.vip.mikuPy2001.bukkit.gui.util;

import com.google.common.base.Charsets;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.logging.Level;
import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.event.HandlerList;
import org.bukkit.plugin.java.JavaPlugin;

/**
 *
 * @author MukiPy2001
 */
public class MyJavaPlugin extends JavaPlugin {

    private HashMap<String, FileConfiguration> Configs = new HashMap<>();

    @Override
    public void onLoad() {
        Instance = this;
        Message.setPix(Instance);
        if (new File(getDataFolder(), "debug").exists())
            Message.setLog(Message.debug);
    }

    @Override
    public void onDisable() {
        Configs = new HashMap<>();
        HandlerList.unregisterAll();//卸载本插件全部监听器        
    }

    @Override
    public FileConfiguration getConfig() {
        return getConfig("config");
    }

    public FileConfiguration getConfig(String conName) {
        if (!Configs.containsKey(conName))
            reloadConfig(conName);
        return Configs.get(conName);
    }

    @Override
    public void reloadConfig() {
        reloadConfig("config");
    }

    public void reloadConfig(String conName) {
        FileConfiguration newConfig = null;
        try (FileInputStream in = new FileInputStream(new File(getDataFolder(), conName + ".yml"));) {
            // 读取数据文件夹里的配置
            newConfig = loadString(in);
        } catch (Exception e) {
            getLogger().log(Level.WARNING, "Unexpected failure reading " + conName + ".yml", e);
        }
        try {
            // 将jar包内的配置文件设为 默认 配置
            InputStream defConfigStream = getResource(conName + ".yml");
            if (defConfigStream != null)
                if (newConfig == null)
                    newConfig = loadString(defConfigStream);
                else
                    newConfig.setDefaults(loadString(defConfigStream));
        } catch (Exception e1) {
            getLogger().log(Level.WARNING, "Unexpected failure reading " + conName + ".yml in jar", e1);
        }
        Configs.put(conName, newConfig);
    }

    @Override
    public void saveDefaultConfig() {
        saveDefaultConfig("config");
    }

    public void saveDefaultConfig(String config) {
        if (!new File(getDataFolder(), config + ".yml").exists())
            saveResource(config + ".yml", false);
    }

    private YamlConfiguration loadString(InputStream inputStream) throws IOException, InvalidConfigurationException {
        YamlConfiguration defYaml = new YamlConfiguration();
        InputStreamReader in = new InputStreamReader(inputStream, Charsets.UTF_8);
        char[] t = new char[2048];
        int i;
        StringBuilder sb = new StringBuilder();
        while ((i = in.read(t, 0, 2048)) > 0)
            sb.append(t, 0, i);
        defYaml.loadFromString(sb.toString());
        return defYaml;
    }

    public static MyJavaPlugin getInstance() {
        return Instance;
    }
    private static MyJavaPlugin Instance;

    /**
     * 第四次修订版. 真正兼容全版本..
     *
     * @author 初音Py2001
     * @return 在线玩家数组
     */
    public static List<Player> getPlayers() {
        ArrayList<Player> players = new ArrayList<>();
        for (World world : Bukkit.getWorlds())
            players.addAll(world.getPlayers());
        return players;
    }
}
